home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / PROGRAMS / CH13 / 13-3-2D.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1998-11-02  |  2.0 KB  |  71 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CDeckOfCards"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Private m_deck(1 To 52) As CCard
  15. Public Event Shuffling(n As Integer, nMax As Integer)
  16.  
  17. Private Sub Class_Initialize()
  18.   Dim i As Integer
  19.   For i = 1 To 52
  20.     Set m_deck(i) = New CCard
  21.     ' Make the first thirteen cards hearts, the
  22.     ' next thirteen cards diamonds, and so on.
  23.     Select Case i
  24.       Case Is <= 13
  25.         m_deck(i).Suit = "Hearts"
  26.       Case Is <= 26
  27.         m_deck(i).Suit = "Diamonds"
  28.       Case Is <= 39
  29.         m_deck(i).Suit = "Clubs"
  30.       Case Else
  31.         m_deck(i).Suit = "Spades"
  32.     End Select
  33.     ' Assign numbers from 1 through 13 to the
  34.     ' cards of each suit.
  35.     If (i Mod 13 = 0) Then
  36.         m_deck(i).Denomination = str(13)
  37.       Else
  38.         m_deck(i).Denomination = str(i Mod 13)
  39.     End If
  40.   Next i
  41. End Sub
  42.  
  43. Public Function ReadCard(cardNum As Integer) As String
  44.   ReadCard = m_deck(cardNum).IdentifyCard
  45. End Function
  46.  
  47. Private Sub Swap(ByVal i As Integer, ByVal j As Integer)
  48.   'Swap the ith and jth card in the deck
  49.   Dim TempCard As New CCard
  50.   TempCard.Denomination = m_deck(i).Denomination
  51.   TempCard.Suit = m_deck(i).Suit
  52.   m_deck(i).Denomination = m_deck(j).Denomination
  53.   m_deck(i).Suit = m_deck(j).Suit
  54.   m_deck(j).Denomination = TempCard.Denomination
  55.   m_deck(j).Suit = TempCard.Suit
  56. End Sub
  57.  
  58. Public Sub ShuffleDeck()
  59.   'Do 200 passes through the deck. On each pass
  60.   'swap each card with a randomly selected card.
  61.   Dim index As Integer, i As Integer, k As Integer
  62.   Randomize  'Initialize random number generator
  63.   For i = 1 To 200
  64.     For k = 1 To 52
  65.       index = Int((52 * Rnd) + 1)
  66.       Call Swap(k, index)
  67.     Next k
  68.     RaiseEvent Shuffling(i, 200)
  69.   Next i
  70. End Sub
  71.